﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using WindowsFormsApp3.Properties;
using System.Media;



namespace WindowsFormsApp3
{
    class Game
    {
        //定义尺寸参数
        private const int GameWidth = 6000;
        private const int GameHeight = 333;
        //定义血条的坐标
        private Point[] bloodPoints =
        {
            new Point(10,10),
            new Point(30,10),
            new Point(50,10),
            new Point(70,10),
            new Point(90,10),
            new Point(110,10),
            new Point(120,10),
        };       

        /// <summary>
        /// 游戏自带的时间
        /// </summary>
        public System.Timers.Timer Timer { get; set; }
        
        //定义委托
        public delegate void GameChanged();
        //定义事件
        public event GameChanged gameChanged;
        
        //定义墙集合
        public List<Wall> walls { get; set; }

        //定义产生时间（墙和心）
        private DateTime nextWallTime { get; set; }
        private DateTime nextHeartTime { get; set; }

        //定义鸟儿
        private Bird bird;

        //定义心集合
        private List<Heart> hearts { get; set; }

        //定义血条集合
        private List<Blood> bloods;

        //定义初始生命值
        private int Initial = 1;

        public Game()
        {
            //初始化产生柱子的时间
            nextWallTime = System.DateTime.Now.AddSeconds(0.9);
            //初始化柱子集合
            walls = new List<Wall>();
            //初始化小鸟
            bird = new Bird(new Rectangle(170, 100, 55, 55));
            //初始化心集合
            hearts = new List<Heart>();
            //初始化血条集合
            bloods = new List<Blood>();
            //初始化血条
            foreach (var bloodPoint in bloodPoints)
            {
                Blood blood = new Blood(new Rectangle(bloodPoint, new Size(20, 20)));
                bloods.Add(blood);
            }
            //初始化下次产生心时间
            nextHeartTime = System.DateTime.Now.AddSeconds(8);
            //初始化时钟
            Timer = new System.Timers.Timer();
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();            
        }

        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            bird.KeyDown(key);
        }

        /// <summary>
        /// 绘制游戏场景
        /// </summary>
        /// <param name="g">绘图句柄</param>
        /// <param name="size">游戏区域尺寸</param>
        public void Draw(Graphics g, Size size)
        {
            //定义了画布的大小
            size = new Size(GameWidth, GameHeight);

            //定义一张画布
            Image img = new Bitmap(GameWidth, GameHeight);

            //在指定的画布上绘图
            Graphics _g = Graphics.FromImage(img);

            //定义了一个指定大小的矩形
            Rectangle rec1 = new Rectangle(0, 0,GameWidth/2,333);
            
            //定义了第二个矩形
            Rectangle rec2 = new Rectangle(3000, 0, GameWidth / 2, 333);

            //定义了画的方式：位置和内容
            _g.DrawImage(Resources.BG, rec1);

            //定义了第二张画
            _g.DrawImage(Resources.BG2, rec2);

            //定义了画布在控件上的的位置和大小
            g.DrawImage(img, new Rectangle(new Point(X, 0), size));

            //引用bird
            bird.Draw(g);

            //逐个绘制墙
            for (int i = 0; i < walls.Count; i++)
            {
                if (walls[i] != null)
                {
                    if (X>=-2800)
                    {
                        walls[i].Drawcactus(g);
                    }
                    else
                    {
                        walls[i].Drawice(g);
                    }
                }
            }
            //逐个绘制心
            for (int i = 0; i < hearts.Count; i++)
            {
                if (hearts[i] != null)
                {
                    if (X>=-3000)
                    {
                        hearts[i].Drawheart(g);
                    }
                    else
                    {
                        hearts[i].Drawsnow(g);
                    }
                }
            }
            //逐个绘制血条
            for (int i = 0; i < Initial-1; i++)
            {
                if (bloods[i]!=null)
                {
                    bloods[i].Draw(g);
                }
            }
        }
        //时钟处理程序
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Timer.Stop();

            //碰墙音频

            foreach (var wall in walls)
            {
                if (wall.HandleTouch(bird.Rectangle))
                {
                    SoundPlayer player = new SoundPlayer(Properties.Resources.啾);
                    player.Play();
                }
            }

            //吃掉buff音频
            foreach (var heart in hearts)
            {
                if (heart.HeartTouch(bird.Rectangle))
                {
                    SoundPlayer player = new SoundPlayer(Properties.Resources.吃掉);
                    player.Play();
                }
            }

            //执行Move函数
            Move();

            //产生新的柱子
            if (System.DateTime.Now > nextWallTime)
            {
                //每个新墙的坐标
                Rectangle rectangle = new Rectangle(350, 0, 42, 400);
                //把产生的墙放入集合中
                if (X>=-2100-3000)
                {
                    walls.Add(new Wall(rectangle));
                    nextWallTime = System.DateTime.Now.AddSeconds(1.5);
                }                
            }
            //产生新的心
            if (System.DateTime.Now>nextHeartTime)
            {
                Rectangle rectangle = new Rectangle(350, 0, 40, 40);
                hearts.Add(new Heart(rectangle));
                nextHeartTime = System.DateTime.Now.AddSeconds(10);
            }
            //墙动
            for (int i = 0; i < walls.Count; i++)
            {
                if (walls[i] != null)
                {
                    walls[i].WallMove();
                }
            }

            //移除墙
            foreach (var wall in walls)
            {
                if (wall.WallRemove())
                {
                    wallNow = wall;
                    walls.Remove(wallNow);                   
                    break;
                }
            }

            //心动
            for (int i = 0; i < hearts.Count; i++)
            {
                if (hearts[i] != null)
                {
                    hearts[i].HeartMove();
                }
            }

            //引用Life函数
            Life();

            //鸟动
            bird.birdMove();

            //事件
            if (gameChanged != null)
            {
                gameChanged();
            }

            Timer.Start();
        }

        /// <summary>
        /// 胜利
        /// </summary>
        /// <returns></returns>
        public bool Victory()
        {
            if (X==-2420-3000)
            {
                Timer.Stop();
                return true;
                
            }
            else
            {
                return false;
            }
        }       
        
        Wall wallNow = null;

        Heart heartNow = null;
        //生命值
        public void Life()
        {
            //处理碰撞（消墙，生命值减一）
            foreach (var wall in walls)
            {
                if (wall.HandleTouch(bird.Rectangle) )
                {
                    wallNow = wall;
                    walls.Remove(wallNow);
                    Initial -= 1;
                    break;
                }
            }
            //处理碰撞（消心，生命值加一）
            foreach (var heart in hearts)
            {
                if (heart.HeartTouch(bird.Rectangle) == true)
                {
                    heartNow = heart;
                    hearts.Remove(heartNow);
                    Initial += 1;
                    break;
                }
            }
        }

        /// <summary>
        /// 游戏结束
        /// </summary>
        /// <returns></returns>
        public bool GameOver()
        {
            bool f = false;
            //生命值为零，游戏结束
            if (Initial == 0)
            {
                Timer.Stop();
                f = !f;
            }
            return f;
        }

        public int X=0;

        //背景图移动
        public void Move()
        {
            X -= 10;
        }

    }
}
